from PIL import Image as PIL_Image, ImageTk
import tkinter as tk
import os
import time as t
# os.path.dirname(__file__)获取当前文件路径
imgPath = f'{os.path.dirname(__file__)}/img/'
files = os.listdir(imgPath)
img = PIL_Image.open(imgPath + '0.png')
(x, y) = img.size  # 获取图片宽度和高度


def showTime():
    a = 11
    myWindow = tk.Tk()
    myWindow.title("北京时间")
    canvas = tk.Canvas(myWindow, width=8*x, height=y, bg='white')  # 创建画布
    for i in Forever():
        (h, m, s) = GetTime()
        h1 = Dispose(h)
        m1 = Dispose(m)
        s1 = Dispose(s)
        num = h1 + m1 + s1
        imgs0 = imgPath + files[num[0]]
        img0 = ImageTk.PhotoImage(file=imgs0)  # 将图片转换成Canvas能够显示的图片类型
        imgs1 = imgPath + files[num[1]]
        img1 = ImageTk.PhotoImage(file=imgs1)
        imgs2 = imgPath + files[num[2]]
        img2 = ImageTk.PhotoImage(file=imgs2)
        imgs3 = imgPath + files[num[3]]
        img3 = ImageTk.PhotoImage(file=imgs3)
        imgs4 = imgPath + files[num[4]]
        img4 = ImageTk.PhotoImage(file=imgs4)
        imgs5 = imgPath + files[num[5]]
        img5 = ImageTk.PhotoImage(file=imgs5)
        imgs6 = imgPath + files[a]
        img6 = ImageTk.PhotoImage(file=imgs6)
        canvas.create_image((x / 2, y / 2), image=img0)  # 显示图片
        canvas.create_image((1.5*x, y / 2), image=img1)
        canvas.create_image((2.5*x, y / 2), image=img6)
        canvas.create_image((3.5*x, y / 2), image=img2)
        canvas.create_image((4.5*x, y / 2), image=img3)
        canvas.create_image((5.5*x, y / 2), image=img6)
        canvas.create_image((6.5*x, y / 2), image=img4)
        canvas.create_image((7.5*x, y / 2), image=img5)
        canvas.pack()
        myWindow.update()
        myWindow.after(1000)  # 图片播放间隔
        a += 1
        if(a == 13):
            a = 11
    myWindow.mainloop()


class Forever:
    def __iter__(self):
        return self

    def __next__(self):
        return None


def Dispose(num):  # 将数值的十位和个位分开
    digit = []
    if (num < 10):
        ten = 0
        unit = num
        digit = [ten, unit]
    else:
        num = str(num)
        for i in num:
            digit.append(int(i))
    return digit


def GetTime():
    local_time = t.localtime(t.time())
    hour = int("%02u" % (local_time.tm_hour))
    minute = int("%02u" % (local_time.tm_min))
    second = int('%02u' % (local_time.tm_sec))
    return (hour, minute, second)


showTime()
